<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
function Node (value) {
    this.value = value
    this.next = null
}

function Link () {
    this.head = new Node()
}

Link.prototype.add = function (value) {
    let current = this.head
    while(current.next) {
        current = current.next
    }
    current.next = new Node(value)
}

Link.prototype.addAt = function (index, value) {
    let current = this.head
    let currentIndex = 0
    while(currentIndex !== index) {
        current = current.next
        currentIndex++
    }
    tmpNode = current.next
    let newNode = new Node(value)
    current.next = newNode
    newNode.next = tmpNode
}

let link1 = new Link()
link1.add(1)
link1.add(2)
link1.addAt(1, 4)
console.log(link1)
</script>
</body>
</html>